#include "slideshowdlg.h"
#include "ui_slideshowdlg.h"
#include "prelistwid.h"
#include "protreewidget.h"

SlideShowDlg::SlideShowDlg(QWidget *parent, QTreeWidgetItem *first_item, QTreeWidgetItem *last_item)
    : QDialog(parent), _first_item(first_item), _last_item(last_item),
    ui(new Ui::SlideShowDlg)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);     // 无边框对话框
    ui->slidepreBtn->SetIcons(":/icon/previous.png",
                              ":/icon/previous_hover.png",
                              ":/icon/previous_press.png");
    ui->slidenextBtn->SetIcons(":/icon/next.png",
                               ":/icon/next_hover.png",
                               ":/icon/next_press.png");
    ui->closeBtn->SetIcons(":/icon/closeshow.png",
                          ":/icon/closeshow_hover.png",
                          ":/icon/closeshow_press.png");
    ui->playBtn->SetIcons(":/icon/play.png",
                          ":/icon/play_hover.png",
                          ":/icon/play_press.png",
                          ":/icon/pause.png",
                          ":/icon/pause_hover.png",
                          ":/icon/pause_press.png");

    connect(ui->closeBtn, &QPushButton::clicked, this, &SlideShowDlg::close);     // 关闭信号
    connect(ui->slidenextBtn, &QPushButton::clicked, this, &SlideShowDlg::SlotSlideNext);     // 向后滑动
    connect(ui->slidepreBtn, &QPushButton::clicked, this, &SlideShowDlg::SlotSlidePre);         // 向前滑动
    // 需要把信号连接放在图片设置前，否则会出现预览小图缺少前两张
    auto *prelistWid = dynamic_cast<PreListWid*>(ui->preListWidget);       // 找到预览列表
    connect(ui->picAnimation, &PicAnimationWid::SigUpPreList,
            prelistWid, &PreListWid::SlotUpPreList);      // 更新预览窗口
    connect(ui->picAnimation, &PicAnimationWid::SigSeleteItem,
            prelistWid, &PreListWid::SlotUpSelect);      // 更新预览窗口
    connect(prelistWid, &PreListWid::SigUpSelectShow, ui->picAnimation,
            &PicAnimationWid::SlotUpSelectShow);
    connect(ui->playBtn, &PicStateBtn::clicked, ui->picAnimation,
            &PicAnimationWid::SlotStartOrStop);
    connect(ui->picAnimation, &PicAnimationWid::SigStart, ui->playBtn,
            &PicStateBtn::SlotStart);
    connect(ui->picAnimation, &PicAnimationWid::SigStop, ui->playBtn,
            &PicStateBtn::SlotStop);
    auto *_protree_widget = dynamic_cast<ProTreeWidget*>(parent);
    connect(ui->picAnimation, &PicAnimationWid::SigStartMusic, _protree_widget, &ProTreeWidget::SlotStartMusic);        // 启动音乐
    connect(ui->picAnimation, &PicAnimationWid::SigStopMusic, _protree_widget, &ProTreeWidget::SlotStopMusic);          // 停止音乐
    connect(ui->closeBtn, &QPushButton::clicked, _protree_widget, &ProTreeWidget::SlotStopMusic);       // 关闭轮播图时停止音乐

    ui->picAnimation->SetPixmap(_first_item);       // 获取第一张图片

    ui->picAnimation->Start();
}

SlideShowDlg::~SlideShowDlg()
{
    delete ui;
}

void SlideShowDlg::SlotSlideNext()
{
    ui->picAnimation->SlideNext();      // 调用向后滑动函数
}

void SlideShowDlg::SlotSlidePre()
{
    ui->picAnimation->SlidePre();       // 调用向前滑动函数
}
